﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Interface;
using Hammock;
using Payton.WP7.Common.Model;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using Hammock.Authentication.OAuth;

namespace Payton.WP7.Common.Sina
{
    public class SinaUserService : UserServiceBase
    {

        protected override void Initialize()
        {
            GET_CURRENT_USER = "/account/verify_credentials.json";
            GET_USER = "/users/show.json";
            FOLLOW_USER = "/friendships/create.json";
            UNFOLLOW_USER = "/friendships/destroy.json";
            FRIENDSHIP = "/friendships/show.json";
            //GET_USER_LIST = "/api/user/infos?format=json";
            //GET_FANS = "/api/friends/fanlist?format=json";
            //GET_FANS_LITE = "/api/friends/fanlist_s?format=json";
            //GET_IDOLS = "/api/friends/idollist?format=json";
            //GET_IDOLS_LITE = "/api/friends/idollist_s?format=json";
            GET_USER_IDOLS = "statuses/friends.json";
            GET_USER_FANS = "statuses/followers.json";
        }

        protected override string[] GetParamtersList(Model.SNSType type)
        {
            string[] list;
            switch (type)
            {
                case SNSType.SingleUser:
                    list = new string[] { 
                      SNSParametersConstants.USER_ID,
                      SNSParametersConstants.SCREEN_NAME
                    };
                    break;
                case SNSType.UserFriends:
                    list = new string[] { 
                        SNSParametersConstants.USER_ID,
                        SNSParametersConstants.CURSOR ,
                        SNSParametersConstants.COUNT
                    };
                    break;
                case SNSType.UserFollowers:
                    list = new string[] { 
                        SNSParametersConstants.USER_ID,
                        SNSParametersConstants.CURSOR ,
                        SNSParametersConstants.COUNT
                    };
                    break;
                case SNSType.HotPeople:
                    return new string[]
                    { 
                        
                    };
                 case SNSType.FriendShip:
                    return new string[]
                    { 
                        SNSParametersConstants.TARGET_ID,
                        SNSParametersConstants.SOURCE_ID
                    };
                case SNSType.Follow:
                    return new string[]
                    { 
                        SNSParametersConstants.USER_ID
                    };
                   
                case SNSType.UnFollow:
                    return new string[]
                    { 
                        SNSParametersConstants.USER_ID
                    };
                   
                case SNSType.SuggestionPeoples:
                    list = new string[] { 
                        //SNSParametersConstants.COUNT,
                        //SNSParametersConstants.BASE_APP
                    };
                    break;
                default:
                    list = new string[] { };
                    break;
            }
            return list;
        }

        protected override System.Collections.Generic.IList<Model.SNSUser> Mapping(string res, SNSResult result)
        {
            JObject o = JObject.Parse(res);

            List<Model.SNSUser> users = o.SelectToken("users").Select(
                s => factory.GetMapping<JToken, Model.SNSUser>().Map(s)
                ).ToList();

            if (o.SelectToken("next_cursor") != null)
                result.PagingInfo = new SNSPagingInfo() { Cursor = (int)o.SelectToken("next_cursor"), HasNext = (int)o.SelectToken("next_cursor") ==0?false:true};
            return users;
        }

        protected override SNSUser MappingSingle(string res, SNSResult result)
        {
            JObject o = JObject.Parse( res );

            return factory.GetMapping<JToken, Model.SNSUser>().Map(o);
        }

        protected override T MappingSingle<T>(string res, SNSResult result)
        {
            JObject o = JObject.Parse(res);

            return factory.GetMapping<JToken,T>().Map(o);
        }

        protected override int ValidateResponse(RestResponse res, out string msg)
        {
            if (res.Content.StartsWith("<"))
            {
                msg = res.Content;
                return 0;
            }

            if (string.IsNullOrEmpty(res.Content))
            {
                msg = "no data";
                return 99;
            }

            if (res.Content.Contains("error"))
            {
                msg = res.Content;
                //TO-DO HANDLE ERROR
                return 0;
            }
            msg = "ok";

            return 1;
        }

        public override void Follow(SNSUser user, Action<SNSUser, SNSResult> action)
        {
            base.Follow(user, action);
        }

        public override void Unfollow(SNSUser user, Action<SNSUser, SNSResult> action)
        {
            base.Unfollow(user, action);
        }
    }
}
